$(function() {

    var that;

    //var log;
    //log = console.log;

    function hideContent(){
         $('#text_cont').hide()
    }
    
    tinyMCE.init({

        mode : "textareas",
        theme : "advanced",
        plugins : "lists,inlinepopups,paste,directionality,xhtmlxtras",
        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,outdent,indent,|,undo,redo,|,link,unlink,anchor,image,cleanup",
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_resizing : false,
        content_css : base_url+"assets/js/tinymce/themes/advanced/skins/default/custom_content.css",
        oninit : hideContent

    });

    $('#page_id').change(function(){

        that = $(this).busy({ position : 'right', hide : false });
        $('#text_cont').hide();

        $.ajax({
            type: 'POST',
            url: base_url+'cms/view_text',
            data:{ page_id: $(this).val() },
            dataType: 'json',
            success: function(data){ 

                if (typeof data.row[0]==='undefined'){                                       
                    that.busy('hide');
                    logm(language['nodata']);
                    $('#text_cont input[name=title]').val('');
                    tinyMCE.get('txt_strane').setContent('');
                    $('#text_cont').fadeIn();

                }else{

                    //log(data.row[0])
                    $('#text_cont').fadeIn();
                    that.busy('hide'); 

                    $('#text_cont input[name=title]').val(data.row[0].title);
                    tinyMCE.get('txt_strane').setContent(data.row[0].text);


                }



            },
            error:function(data){alert("Error: " + data);}
        });
        
        return false;

    })      

    $('#form_submit').live('click', function(){

        that = $(this).busy({ position : 'left', hide : false });
        
        var page_id = $('#page_id').val();
        var title = $('#text_cont input[name=title]').val();
        var txt = tinyMCE.get('txt_strane').getContent();

        if(title=='') {
            logm(language['enter']+' <b>'+language['title']+'</b>!');
            return false;
        }

        if(txt=='') {
            logm(language['enter']+' <b>'+language['text']+'</b>!');
            return false;
        }

        /*log('page_id: '+ page_id);
        log('title: '+ title);
        log('text: '+ txt);*/

        $.ajax({
            type: 'POST',
            url: base_url+'cms/edit_text',
            data:{ 
                page_id: page_id,
                title: title,
                text: txt
            },
            dataType: 'json',
            success: function(data){

                if(data.action==true){
                    $('#text_cont').hide();
                    $('#page_id').val(0); 
                    logm(language['success']);
                    
                }else{
                    logm(data.msg);
                }


            },
            error:function(data){alert("Error: " + data);}
        });
        
        return false;

    });


    function logm($msg){

        $('.logm').remove();
        that.busy('hide'); 
        
        var html = '<div class="box clearfix logm" style="display:none;border-color:red"><h4>'+language['systemmessage']+'</h4><p></p></div>';

        $('.box:first').after(html);
        $('.box:nth-child(2) p').html($msg);
        $('.box:nth-child(2)').fadeIn('slow');
        
        $('html, body').animate({
            scrollTop: $(".box:nth-child(2)").offset().top
        }, 500);


        setTimeout("$('.box:nth-child(2)').fadeOut(function(){$(this).remove();})",2300);
    }


});

                            